'use strict';

module.exports = function(server) {
  const SysUser = server.models.SysUser;
  const SysRole = server.models.SysRole;
  const Page = server.models.Page;

  const now = new Date().getTime();
  const adminUser = {
    "username": "admin",
    "password": "Q!W@e3r4",
    "email": "admin@opsmart.com",
    "status": 1,
    "created_time": now
  };
  const adminRole = {
    "code": "000000",
    "name": "系统管理员",
    "created_date": now
  };
  const defaultModules = [
    { "code": "01", "name": "综合管理中心", "url": "/general", "view_order": 2, "menu_icon": "setting", "page_type": 0, "status": 1, "memo": "", "published_time": now },
    { "code": "02", "name": "任务管理中心", "url": "/todo", "view_order": 1, "menu_icon": "setting", "page_type": 0, "status": 1, "memo": "", "published_time": now },
  ];
  const defaultPages = [
    { "code": "0101", "name": "系统用户管理", "url": "/general/user", "view_order": 1, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
    { "code": "0102", "name": "系统角色管理", "url": "/general/role", "view_order": 2, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
    { "code": "0103", "name": "菜单管理", "url": "/general/menu", "view_order": 3, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
    { "code": "0104", "name": "组织机构", "url": "/general/org", "view_order": 4, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
    { "code": "0105", "name": "人员管理", "url": "/general/person", "view_order": 5, "menu_icon": "", "page_type": 1, "status": 1, "memo": "", "published_time": now },
  ];

  const setDefaultUserPromise = () => {
    return new Promise((resolve, reject) => {
      SysUser.findOrCreate({ where: { name: adminUser.name }}, adminUser, function(err, user, created) {
        if (created) {
          resolve(user);
          console.log('default admin user created.');
        } else if (err) {
          reject(err);
          console.log(err);
        }
      });
    });
  };
  const setDefaultRolePromise = () => {
    return new Promise((resolve, reject) => {
      SysRole.findOrCreate({ where: { code: adminRole.code }}, adminRole, function(err, role, created) {
        if (created) {
          resolve(role);
          console.log('default admin role created.');
        } else if (err) {
          reject(err);
          console.log(err);
        }
      });
    });
  };
  const setDefaultPagesPromise = () => {
    console.log(defaultModules[0]);
    return new Promise((resolve, reject) => {
      Page.findOrCreate({ where: { code: defaultModules[0].code }}, defaultModules, function(err, module, created) {
        if (created) {
          module.forEach(mod => {
            defaultPages.map(page => {
              if (page.code.startsWith(mod.code)) {
                page.parentId = mod.id;
              }
            });
          });
          Page.create(defaultPages, (err, pages) => {
            resolve([...module, ...pages]);
          });
          console.log('default pages created.');
        } else if (err) {
          reject(err);
          console.log(err);
        }
      });
    });
  };
  const allDefaultPromise = [
    setDefaultUserPromise(),
    setDefaultRolePromise(),
    setDefaultPagesPromise()
  ];

  Promise.all(allDefaultPromise).then((res) => {
    const RoleUser = server.models.RoleUser;
    const RolePage = server.models.RolePage;

    const adminUserId = res[0].id;
    const adminRoleId = res[1].id;
    const defaultPages = res[2];

    RoleUser.create({ "userId": adminUserId, "roleId": adminRoleId });
    console.log('default role_user created.');

    let rolePage = [];
    defaultPages.forEach(page => {
      rolePage.push({
        "pageId": page.id,
        "roleId": adminRoleId
      });
    });
    RolePage.create(rolePage);
    console.log('default role_page created.');
  }, (err) => console.log(err));
};
